package com.liantong.service.cms;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.liantong.common.dao.ActivityPhotoDao;
import com.liantong.common.mapper.ActivityMapper;
import com.liantong.common.mapper.ActivityPhotoMapper;
import com.liantong.common.mapper.BusinessHallMapper;
import com.liantong.common.po.Activity;
import com.liantong.common.po.ActivityExample;
import com.liantong.common.po.ActivityPhoto;
import com.liantong.common.po.ActivityPhotoExample;
import com.liantong.common.po.BusinessHall;
import com.liantong.common.po.BusinessHallExample;
import com.liantong.common.vo.InsertActivityInfo;
import com.liantong.service.util.AppUuidGenerator;
import com.liantong.service.util.FileUtil;
import com.liantong.util.MD5Util;

@Service("activityService")
public class ActivityService  {
	
	@Resource(name="activityMapper")
	private ActivityMapper activityMapper;

	@Resource(name="activityPhotoMapper")
	private ActivityPhotoMapper activityPhotoMapper;
	
	@Resource(name="activityPhotoDao")
	private ActivityPhotoDao activityPhotoDao;
	
	@Resource(name="businessHallMapper")
	private BusinessHallMapper bussinessHallMapper;
	
	@Transactional
	public void insertActivity(InsertActivityInfo insertActivityInfo) throws Exception {

		Activity activity = insertActivityInfo.getActivity();
		activity.setId(AppUuidGenerator.generate());
		copyResource(activity, insertActivityInfo);
		activityMapper.insert(activity);
	}
	
	public List<Activity> getAll(){
		ActivityExample example = new ActivityExample();
		example.createCriteria().andIdIsNotNull();
		List<Activity> result = activityMapper.selectByExampleWithBLOBs(example);
		
		BusinessHallExample bhe= new BusinessHallExample();
		
		for (Activity r : result) {
			bhe.clear();
			bhe.createCriteria().andIdEqualTo(r.getBusinessHallId());
			List<BusinessHall> bussinessHall = bussinessHallMapper.selectByExample(bhe);
			r.setHallName(bussinessHall.get(0).getName());
			r.setPhoto(FileUtil.getRealPath(r.getPhoto()));
			ActivityPhotoExample pe = new ActivityPhotoExample();
			pe.createCriteria().andActivityIdEqualTo(r.getId());
			List<ActivityPhoto> list = activityPhotoMapper.selectByExample(pe);
			List<String> paths = new ArrayList<String>();
			for(ActivityPhoto p : list){
				paths.add(FileUtil.getRealPath(p.getPhotoUrl()));
			}
			r.setPictures(paths);
		}
		return result;
	}
		
	
	private void copyResource(Activity activity, InsertActivityInfo installActivityInfo) throws Exception {
		List<String> picturePath = new ArrayList<String>();

		String id = String.valueOf(activity.getId());

		String contextPath = installActivityInfo.getContextPath();

		String photoFilePath = copyFile(id, installActivityInfo.getPhotoPath(), contextPath, installActivityInfo.getPhotoName(),
				installActivityInfo.getPhoto());
		activity.setPhoto(photoFilePath);

		picturePath = copyFiles(id, installActivityInfo.getPictures(), installActivityInfo.getPictureName(), contextPath,
				installActivityInfo.getPicturePath());


		addPicture(activity.getId(), picturePath);
	}
	
	private String copyFile(String appId, String toParentFilePath, String toContentPath, String fromFileName, File fromFile) throws Exception {

		String toFileName = "";
		try {
			toFileName = toParentFilePath + FileUtil.getDestinationFilePath(appId, MD5Util.getMD5(fromFile), fromFileName);
			FileUtils.copyFile(fromFile, new File(toContentPath + toFileName));

		} catch (IOException e) {
			throw new Exception(e);
		}
		return toFileName;
	}
	
	private List<String> copyFiles(String id, File[] files, String[] fileName, String contextPath, String filePath) throws Exception {

		List<String> result = new ArrayList<String>();

		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			String name = fileName[i];

			String previewFilePath = copyFile(id, filePath, contextPath, name, file);
			result.add(previewFilePath);
		}

		return result;
	}
	
	private void addPicture(String id, List<String> preview) {
		List<ActivityPhoto> list = new ArrayList<ActivityPhoto>();

		for (String respath : preview) {
			ActivityPhoto pv = new ActivityPhoto();

			pv.setId(AppUuidGenerator.generate());
			pv.setActivityId(id);
			pv.setPhotoUrl(respath);

			list.add(pv);			
		}
		activityPhotoDao.batchInsert(list);		
	}

}
